package com.ly.web.controller.erp;

import com.ly.heart.domain.OrderEntity;
import com.ly.heart.domain.dto.ContractOrderMaterialDetailDto;
import com.ly.heart.domain.dto.OrderQueryDto;
import com.ly.heart.domain.vo.select.query.OrderSelectQueryDto;
import com.ly.heart.service.king.OrderPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.service.IOrderService;

import javax.annotation.Resource;

/**
 * 采购订单管理Controller
 * @author cxl
 * @date 2024-05-14
 */
@Api (value = "Order", tags = "采购订单管理")
@RestController
@RequestMapping ("/heart/Order")
public class OrderController extends BaseController {

    @Resource
    private IOrderService orderService;
    @Resource
    private OrderPushService orderPushService;


    @PreAuthorize ("@ss.hasPermi('heart:Order:add')")
    @Log (title = "暂存采购订单", businessType = BusinessType.INSERT)
    @PostMapping("/draft")
    @ApiOperation ("暂存采购订单")
    public AjaxResult add (@RequestBody OrderEntity order) {
        return orderService.draft ( order );
    }

    @PreAuthorize ("@ss.hasPermi('heart:Order:add')")
    @Log (title = "提交采购订单", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    @ApiOperation ("提交采购订单")
    public AjaxResult submit(@RequestBody OrderEntity order){
        return orderService.submit(order);
    }

    @PreAuthorize ("@ss.hasPermi('heart:Order:edit')")
    @Log (title = "修改采购订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation ("修改采购订单")
    public AjaxResult update(@RequestBody OrderEntity order){
        return orderService.update(order);
    }

    @PreAuthorize ("@ss.hasPermi('heart:Order:list')")
    @Log (title = "查询采购订单列表", businessType = BusinessType.OTHER)
    @PostMapping ("/list")
    @ApiOperation ("查询采购订单列表")
    public AjaxResult getOrderPageListByMaterial (@RequestBody OrderQueryDto queryDto) {
        return orderService.getOrderPageListByMaterial ( queryDto );
    }

    @PreAuthorize ("@ss.hasPermi('heart:Order:edit')")
    @Log (title = "修改采购订单,生成采购订单新变更单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation ("修改采购订单,生成采购订单新变更单")
    public AjaxResult edit (@RequestBody OrderEntity order) {
        return orderService.updateOrder ( order );
    }

    @PreAuthorize ("@ss.hasPermi('heart:Order:remove')")
    @Log (title = "删除采购订单", businessType = BusinessType.DELETE)
    @DeleteMapping ("/{ids}")
    @ApiOperation ("删除采购订单")
    public AjaxResult remove (@PathVariable String[] ids) {
        return orderService.removeOrderByIds(ids);
    }

    @PreAuthorize ("@ss.hasPermi('heart:Order:query')")
    @GetMapping (value = "/{id}")
    @ApiOperation ("获取采购订单详细信息")
    public AjaxResult getInfo (@PathVariable ("id") String id) {
        return orderService.getOrderById ( id );
    }

    @PostMapping ("/changeList")
    @ApiOperation ("查询采购订单新变更单列表")
    public AjaxResult getOrderChangeListByMaterial(@RequestBody OrderQueryDto queryDto){
        return orderService.getOrderChangeListByMaterial(queryDto);
    }

    @Log (title = "新增采购合同界面，选择采购订单时，查询采购订单的物料明细列表")
    @GetMapping ("queryMaterial")
    @ApiOperation ("根据采购订单对象List查询物料明细")
    public AjaxResult queryMaterial (ContractOrderMaterialDetailDto contractOrderMaterialDetailDto) {

        return orderService.queryMaterial (contractOrderMaterialDetailDto);
    }

    @PostMapping ("/select")
    @ApiOperation ("采购订单选单接口")
    public AjaxResult getOrderSelect(@RequestBody OrderSelectQueryDto queryDto){
        return orderService.getOrderSelect(queryDto);
    }

    @GetMapping ("/push/{orderId}")
    @ApiOperation ("采购订单推送")
    public AjaxResult push(@PathVariable String orderId){
        orderPushService.push(orderId);
        return AjaxResult.success("推送成功！");
    }


}
